package com.jfinal.code.eclipse.plugin.wizards.ui;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

import com.jfinal.code.eclipse.plugin.i18n.I18n;
import com.jfinal.code.eclipse.plugin.wizards.page.MakeCodeWizardPage;

/**
 * 创建生成策略引导页的一系列UI
 * @author yaolin
 *
 */
public class MakeCodeUI {

	private final MakeCodeWizardPage page;
	private final Shell shell;
	
	public MakeCodeUI(MakeCodeWizardPage page, Shell shell) {
		this.page = page;
		this.shell = shell;
	}
	/**
	 * 创建TabFolder
	 * @param parent
	 */
	public TabFolder createTabFolder(Composite parent) {
		TabFolder tabFolder = new TabFolder(parent, SWT.NULL);
		tabFolder.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (page.isInited()) { // NPE 请吃冰淇淋。
					Validator.validatorEmptyString(page);
				}
			}
		});
		return tabFolder;
	}
	
	// -----------------JFINAL-------------------------
	/**
	 * 代码根目录
	 */
	public Text createCodeText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_codePathLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		// Button
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new BrowseSelectionAdapter("", text));
		return text;
	}
	
	/**
	 * 创建表前缀的根路径Text
	 */
	public Text createPrefixText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_tablePrefixLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		Label tip = new Label(parent, SWT.NULL);
		tip.setText(I18n.makeCodeUI_tablePrefixTip);
		return text;
	}
	
	/**
	 * 创建baseModelPackageNameText
	 */
	public Text createBaseModelPackageNameText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_baseModelPackageNameLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		new Label(parent, SWT.NULL);
		return text;
	}
	
	/**
	 * 创建modelPackageNameText
	 */
	public Text createModelPackageNameText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_modelPackageNameLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		new Label(parent, SWT.NULL);
		return text;
	}
	
	/**
	 * 创建在model中生成dao复选框
	 */
	public Button createGenDaoInModelButton(Composite parent) {
		new Label(parent, SWT.NULL); // 只是占位
		final Button button = new Button(parent, SWT.CHECK);
		button.setText(I18n.makeCodeUI_makeDaoInModelLabel);
		button.setSelection(true); // 默认选中
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		new Label(parent, SWT.NULL);
		return button;
	}
	
	/**
	 * 创建SqlServer 2008报错解决按钮
	 */
	public Button createSqlServerFixErrorButton(Composite parent) {
		new Label(parent, SWT.NULL); // 只是占位
		final Button button = new Button(parent, SWT.CHECK);
		button.setText(I18n.makeCodeUI_sqlServerFixErrorLabel);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		new Label(parent, SWT.NULL);
		return button;
	}
	
	/**
	 * 创建生成DICT复选框
	 */
	public Button createGenDictButton(Composite parent) {
		new Label(parent, SWT.NULL); // 只是占位
		final Button button = new Button(parent, SWT.CHECK);
		button.setText(I18n.makeCodeUI_makeDictLabel);
		button.setSelection(true); // 默认选中
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		new Label(parent, SWT.NULL);
		return button;
	}
	// ------------------------------------------------
	
	
	// -----------------CUSTOM-------------------------
	/**
	 * 创建自定义jarText
	 */
	public Text createCustomJarFileText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_customJarFileLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		// Button
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = new FileDialog(shell, SWT.OPEN);
				dialog.setText(I18n.databaseInfoUIEvent_pleaseSelectDriverFile);
				String fileName = dialog.open();
				text.setText(fileName == null || !fileName.endsWith(".jar") ? "" : fileName);
			}
		});
		return text;
	}
	/**
	 * 创建customImplClassNameText
	 */
	public Text createCustomImplClassNameText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_customImplClassNameLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		new Label(parent, SWT.NULL);
		return text;
	}
	/**
	 * 创建BasePackNameText
	 */
	public Text createBasePackNameText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_basePackNameLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		new Label(parent, SWT.NULL);
		return text;
	}
	
	/**
	 * 创建模板文件的根路径Text
	 */
	public Text createTemplateDirText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_templatePathLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		// Button
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Browse...");
		// button.addSelectionListener(new BrowseSelectionAdapter("", text));
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN);
				dialog.setText(I18n.databaseInfoUIEvent_pleaseSelectDriverFile);
				String path = dialog.open();
				text.setText(path == null ? "" : path);
			}
		});
		return text;
	}

	/**
	 * 创建java文件路径Text
	 */
	public Text createJavaText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_javaPathLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		// Button
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new BrowseSelectionAdapter("", text));
		return text;
	}

	/**
	 * 创建resource文件路径Text
	 */
	public Text createResourceText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_resourcePathLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		// Button
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new BrowseSelectionAdapter("", text));
		return text;
	}

	/**
	 * 创建js文件路径Text
	 */
	public Text createJsText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_jsPathLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		// Button
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new BrowseSelectionAdapter("", text));
		return text;
	}

	/**
	 * 创建视图文件路径Text
	 */
	public Text createViewText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_viewPathLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		// Button
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new BrowseSelectionAdapter("", text));
		return text;
	}
	// ------------------------------------------------
	
	// -----------------JFINAL-CUSTOM------------------
	/**
	 * 创建jfinal自定义jarText
	 */
	public Text createJfinalCustomJarFileText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_customJarFileLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		// Button
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = new FileDialog(shell, SWT.OPEN);
				dialog.setText(I18n.databaseInfoUIEvent_pleaseSelectDriverFile);
				String fileName = dialog.open();
				text.setText(fileName == null || !fileName.endsWith(".jar") ? "" : fileName);
			}
		});
		return text;
	}
	/**
	 * 创建JfinalCustomImplClassNameText
	 */
	public Text createJfinalCustomImplClassNameText(Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_customImplClassNameLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter());
		new Label(parent, SWT.NULL);
		return text;
	}
	// ------------------------------------------------
	/**
	 * 文本校验适配
	 * @author yaolin
	 */
	class ValidatorModifyAdapter implements ModifyListener {
		@Override
		public void modifyText(ModifyEvent e) {
			Validator.validatorEmptyString(page);
		}
	}
	/**
	 * 选择适配
	 * @author yaolin
	 */
	class BrowseSelectionAdapter extends SelectionAdapter {
		private final String title;
		private final Text text;
		public BrowseSelectionAdapter(String title, Text text) {
			this.title = title;
			this.text = text;
		}
		@Override
		public void widgetSelected(SelectionEvent e) {
			ContainerSelectionDialog dialog = new ContainerSelectionDialog(shell, ResourcesPlugin.getWorkspace().getRoot(), false, title);
			if (dialog.open() == ContainerSelectionDialog.OK) {
				Object[] result = dialog.getResult();
				if (result.length == 1) {
					text.setText(((Path) result[0]).toString());
				}
			}
		}
	}
	/**
	 * 非空校验
	 * @author yaolin
	 */
	public static class Validator {
		/**
		 * 控件非空校验
		 */
		public static void validatorEmptyString(MakeCodeWizardPage page) {
			switch (page.getTabFolder().getSelectionIndex()) {
			case 0:
				if (page.getCodeText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_codePathLabel));
					return;
				}
				/*if (page.getPrefixText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_tablePrefixLabel));
					return;
				}*/
				if (page.getBaseModelPackageNameText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_baseModelPackageNameLabel));
					return;
				}
				if (page.getModelPackageNameText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_modelPackageNameLabel));
					return;
				}
				break;
			case 1:
				if (page.getCustomJarFileText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_customJarFileLabel));
					return;
				}
				if (page.getCustomImplClassNameText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_customImplClassNameLabel));
					return;
				}
				if (page.getBasePackNameText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_basePackNameLabel));
					return;
				}
				if (page.getJavaText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_javaPathLabel));
					return;
				}
				if (page.getResourceText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_resourcePathLabel));
					return;
				}
				if (page.getJsText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_jsPathLabel));
					return;
				}
				if (page.getViewText().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_viewPathLabel));
					return;
				}
				break;
			case 2:
				// _10Code
				break;
			}
			updateStatus(page, null);
		}

		/**
		 * 设置错误信息
		 */
		private static void updateStatus(MakeCodeWizardPage page, String message) {
			page.setErrorMessage(message);
			page.setPageComplete(message == null);
		}
	}
}
